package pakete2;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.Point;

public class EI_TxartelaGaitu extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JComboBox jComboBox = null;

	private JTextArea jTextArea = null;

	private JButton jButton = null;

	private KudeatzaileInterfazea kud = null; // @jve:decl-index=0:
	
	private EI_SegurtasunArduraduna aita;

	/**
	 * @param owner
	 */
	public EI_TxartelaGaitu(EI_SegurtasunArduraduna aita) {
		super(aita, true);
		this.aita = aita;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(325, 100);
		this.setLocation(new Point(400, 250));
		this.setContentPane(getJContentPane());
		this.setTitle("Txartela gaitu");

	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setText("Txartelaren kodea sartu:	");
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			Box box = Box.createHorizontalBox();
			Box box2 = Box.createVerticalBox();

			box.add(jLabel);
			box.add(this.getJComboBox());

			box2.add(getJButton());
			box2.add(getJTextArea());

			jContentPane.add(box, BorderLayout.NORTH);
			jContentPane.add(box2, BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JComboBox getJComboBox() {
		try {
			kud = (KudeatzaileInterfazea) Naming
					.lookup("rmi://localhost/kudeatzailea");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Vector<String> txartelKodeak = null;
		try {
			txartelKodeak = kud.getTxartelak();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (jComboBox == null) {
			jComboBox = new JComboBox(txartelKodeak);
		}
		// jComboBox.addActionListener(new java.awt.event.ActionListener() {
		// public void actionPerformed(java.awt.event.ActionEvent e) {
		//
		// KudeatzaileInterfazea kud = null;
		//
		// try {
		// kud = (KudeatzaileInterfazea) Naming
		// .lookup("rmi://localhost/kudeatzailea");
		// } catch (MalformedURLException e2) {
		// // TODO Auto-generated catch block
		// e2.printStackTrace();
		// } catch (RemoteException e2) {
		// // TODO Auto-generated catch block
		// e2.printStackTrace();
		// } catch (NotBoundException e2) {
		// // TODO Auto-generated catch block
		// e2.printStackTrace();
		// }
		//
		// String erabDatuak = null;
		// String txartelkod = (String) jComboBox.getSelectedItem();
		//
		// try {
		// erabDatuak = kud.erabiltzaileDatuakLortu(txartelkod);
		// } catch (RemoteException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// } catch (SQLException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		//
		// // jTextArea.setText("id\tizena\ttxartela\tprofila\n" );
		// jTextArea
		// .setText("id\tizena\ttxartela\tprofila\n" + erabDatuak);
		// jTextArea.setVisible(true);
		// //
		// }
		// });

		return jComboBox;
	}

	/**
	 * This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton("Gaitu");
			jButton.setEnabled(true);
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					KudeatzaileInterfazea kud;
					int x = 0;

					try {
						kud = (KudeatzaileInterfazea) Naming
								.lookup("rmi://localhost/kudeatzailea");

						String txartelkod = (String) jComboBox
								.getSelectedItem();
						try {
							x = kud.GaituTxartela(txartelkod);
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						if (x == 1){
							JOptionPane.showMessageDialog(null, "Txartela gaitu da!");
						}
						else{
							JOptionPane.showMessageDialog(null, "Txartela gaituta dago");
						}
						setVisible(false);
						aita.eguneratu();
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				}
			});
		}
		return jButton;
	}

	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setVisible(false);
		}

		return jTextArea;

	}

} // @jve:decl-index=0:visual-constraint="0,0"
